<?php
App::uses('AppController', 'Controller');
/**
 * Scheduleoptions Controller
 *
 * @property Scheduleoption $Scheduleoption
 * @property PaginatorComponent $Paginator
 */
class ScheduleoptionsController extends AppController {

/**
 * Components
 *
 * @var array
 */
        public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
        public function index() {
                $this->Scheduleoption->recursive = 0;
                $this->set('scheduleoptions', $this->paginate());
        }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function view($id = null) {
                if (!$this->Scheduleoption->exists($id)) {
                        throw new NotFoundException(__('Invalid scheduleoption'));
                }
                $options = array('conditions' => array('Scheduleoption.' . $this->Scheduleoption->primaryKey => $id));
                $this->set('scheduleoption', $this->Scheduleoption->find('first', $options));
        }

/**
 * add method
 * MODIFICADO
 * @return void
 */
        public function add($seccionId = null) {
                if ($this->request->is('post')) {
                        $this->Scheduleoption->create();
                        $scheduleExist = $this->Scheduleoption->find('all', array(
                                'conditions' => array(
                                        'section_id' => $this->request->data['Scheduleoption']['section_id'], 
                                        'schedule' => $this->request->data['Scheduleoption']['schedule'].';'.$this->request->data['Scheduleoption']['auxiliar'])));
                        
                        if(sizeof($scheduleExist) > 0){
                                $this->Session->setFlash('Horario ya se encuentra asociado a la sección');
                                $this->redirect(array('action'=>'add', $this->request->data['Scheduleoption']['section_id']));
                        }else{
                                echo '<pre>';
                                print_r($this->request->data);
                                echo '</pre>';
                                $this->request->data['Scheduleoption']['schedule'] = $this->request->data['Scheduleoption']['schedule'].';'.$this->request->data['Scheduleoption']['auxiliar'];
                                if ($this->Scheduleoption->save($this->request->data)) {

                                        $this->Session->setFlash(__('El horario ha sido guardado correctamente.'));
                                        $this->redirect(array('controller' => 'Sections', 'action' => 'viewSchedule', $this->request->data['Scheduleoption']['section_id']));
                                } else {
                                        $this->Session->setFlash(__('El horario no ha sido guardado. Por favor, intentelo nuevamente.'));
                                }
                        }
                }
                $seccion = $this->Scheduleoption->Section->findById($seccionId);
                $horario = $this->Scheduleoption->findBySectionId($seccionId);
                if(sizeof($horario)> 0)
                        $auxiliar = explode(';',$horario['Scheduleoption']['schedule']);
                else
                        $auxiliar = array();

                if(sizeof($auxiliar) > 1){
                        $this->set('auxiliar', $auxiliar[1]);  
                }else
                        $this->set('auxiliar', '');
                
                $this->set('course', $this->Scheduleoption->Section->Course->findById($seccion['Section']['course_id']));
                $this->set('seccion', $seccion);
        }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function edit($id = null) {
        $this->Scheduleoption->id = $id;
                if (!$this->Scheduleoption->exists($id)) {
                        throw new NotFoundException(__('Invalid scheduleoption'));
                }
                if ($this->request->is('post') || $this->request->is('put')) {
                        if ($this->Scheduleoption->save($this->request->data)) {
                                $this->Session->setFlash(__('The scheduleoption has been saved'), 'flash/success');
                                $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The scheduleoption could not be saved. Please, try again.'), 'flash/error');
                        }
                } else {
                        $options = array('conditions' => array('Scheduleoption.' . $this->Scheduleoption->primaryKey => $id));
                        $this->request->data = $this->Scheduleoption->find('first', $options);
                }
                $sections = $this->Scheduleoption->Section->find('list');
                $users = $this->Scheduleoption->User->find('list');
                $this->set(compact('sections', 'users'));
        }

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
        public function delete($id = null) {
                $this->Scheduleoption->id = $id;
                if (!$this->Scheduleoption->exists()) {
                        throw new NotFoundException(__('Horario invalido'));
                }
                $this->request->onlyAllow('post', 'delete');
                $section_id = $this->Scheduleoption->findById($id)['Scheduleoption']['section_id'];
                if ($this->Scheduleoption->delete()) {
                        $this->Session->setFlash(__('El horario ha sido desvinculado de la sección con exito.'));
                } else {
                        $this->Session->setFlash(__('El horario no ha podido ser desvinculado. Por favor, intentelo nuevamente.'));
                }
                
                $this->redirect(array('controller' => 'Sections', 'action' => 'viewSchedule', $section_id));
        }



        public function addExcel($data){
                $this->Scheduleoption->create();
                $this->Scheduleoption->save($data);
        }

        

}

?>